package com.sig.sigandroidDisconnected;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

import com.sig.load.LoadAllElements;
import com.sig.load.LoadPolygons;
import com.sig.sigandroid.R;
import com.sig.signomadeConnected.ConnectedActivity;

public class FirstActivity extends Activity {

	Button bconnect;
	static LoadPolygons lp;
	Dialog progress;
	private ProgressDialog pDialog;
	public static final int progress_bar_type = 0;
	boolean mExternalStorageAvailable = false;
	boolean mExternalStorageWriteable = false;
	static final String PATH_TO_FILE_POLYGONS = "/storage/sdcard1/mapSig/polygons_rel.xml";
	static final String PATH_TO_FILE_ROADS = "/storage/sdcard1/mapSig/roads_rel.xml";
	static final String KEY = "allgraphics";
	String state = Environment.getExternalStorageState();
	static LoadAllElements allgraphics;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_first);
		new LoadData().execute();
	}

	/**
	 * Creat menu
	 */
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.first, menu);
		return true;
	}

	public void loadData(View v) throws IOException {
		Intent intent = new Intent(this, ConnectedActivity.class);
		this.startActivity(intent);
	}

	public void afficheNode(View v) {
		Intent intent = new Intent(this, DisconnectedActivity.class);
		this.startActivity(intent);
	}

	static LoadAllElements getGraphics() {
		return allgraphics;
	}

	/**
	 * Background AsynchTask to loag file
	 * 
	 */
	class LoadData extends AsyncTask<String, String, String> {

		/**
		 * Before starting background thread Show Progress Bar Dialog
		 * */
		@SuppressWarnings("deprecation")
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			showDialog(progress_bar_type);
		}

		@Override
		protected String doInBackground(String... file) {
			if (Environment.MEDIA_MOUNTED.equals(state)) {
				mExternalStorageAvailable = mExternalStorageWriteable = true;

				/*
				 * Resources r = getResources(); InputStream[] in = {
				 * r.openRawResource(R.xml.polygons),
				 * r.openRawResource(R.xml.roads) };
				 */
				File data1 = new File(PATH_TO_FILE_POLYGONS);
				File data2 = new File(PATH_TO_FILE_ROADS);
				File[] data = { data1, data2 };
				// new LoadPolygons(data1);
				allgraphics = new LoadAllElements(data);
				Log.d("Tag", "" + allgraphics.getAllSegments().size());
				Log.d("Tag", "" + allgraphics.getAllTriangles().size());

			} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
				// We can only read the media
				mExternalStorageAvailable = true;
				mExternalStorageWriteable = false;
			} else {

			}
			return null;
		}

		/**
		 * Updating progress bar
		 * */
		protected void onProgressUpdate(String... progress) {
			// setting progress percentage
			pDialog.setProgress(Integer.parseInt(progress[0]));
		}

		@Override
		protected void onPostExecute(String file_url) {
			// dismiss the dialog after the file was downloaded
			dismissDialog(progress_bar_type);

		}

	}

	/**
	 * Showing Dialog
	 * */
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case progress_bar_type: // we set this to 0
			pDialog = new ProgressDialog(this);
			pDialog.setMessage("Loading file. Please wait...\n"
					+ PATH_TO_FILE_POLYGONS + " " + PATH_TO_FILE_ROADS);
			pDialog.setIndeterminate(false);
			pDialog.setProgressStyle(ProgressDialog.THEME_DEVICE_DEFAULT_DARK);
			pDialog.setCancelable(false);
			pDialog.show();
			return pDialog;
		default:
			return null;
		}
	}

}
